﻿using lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReverseList
{
    class Program
    {
        static void Main(string[] args)
        {
            var head = new ListNode(1);
            head.next = new ListNode(2);
            head.next.next = new ListNode(3);
            head.next.next.next = new ListNode(4);
            head.next.next.next.next = new ListNode(5);
            var temp = ReverseList(head);
            Print(temp);
            Console.ReadKey();
        }
        public static void Print(ListNode node)
        {
            Console.WriteLine(node.val);
            if (node.next != null)
            {
                Print(node.next);
            }
        }
        public static ListNode ReverseList(ListNode head)
        {
            if (head == null) return null;
            ListNode newNode = null;
            ListNode currentNode = head;
            while (currentNode != null)
            {
                var temp = currentNode.next;
                currentNode.next = newNode;
                newNode = currentNode;
                currentNode = temp;
            }
            
            return newNode;
        }
        public static ListNode ReverseList1(ListNode head)
        {
            if (head == null || head.next == null) return head;
            var currentNode= ReverseList1(head.next);
            head.next.next = head;
            head.next = null;
            return currentNode;
    
        }
    }


}
